package com.misday.eatup.model;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import android.content.ContentValues;
import android.util.Log;

public class FoodsHandler extends DefaultHandler {
	
	private static final String TAG = "FoodsHandler";
	
	private List<ContentValues> mConflictList;
	private List<ContentValues> mUnsuitableList;
	
	private static final String ITEM = "item";

	private ContentValues item;
	
	private String tagName;
	private String tabName;
	
	FoodsHandler() {
		super();
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
	}
	
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		tagName = localName;
		
		//Log.i(TAG, "<" + tagName + ">");
		
		if (FoodsDatabaseHelper.CONFLICT_TABEL_NAME.equals(tagName)) {
			mConflictList = new ArrayList<ContentValues>();
			tabName = tagName;
		} else if (FoodsDatabaseHelper.UNSUITABLE_TABEL_NAME.equals(tagName)) {
			mUnsuitableList = new ArrayList<ContentValues>();
			tabName = tagName;
		} else if (ITEM.equals(tagName)) {
			if (FoodsDatabaseHelper.CONFLICT_TABEL_NAME.equals(tabName) 
			 || FoodsDatabaseHelper.UNSUITABLE_TABEL_NAME.equals(tabName)) {
				item =  new ContentValues();
			}
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		tagName = localName;
		
		//Log.i(TAG, "</" +tagName + ">");
		
		if (FoodsDatabaseHelper.CONFLICT_TABEL_NAME.equals(tagName)) {
			tabName = "";
		} else if (FoodsDatabaseHelper.UNSUITABLE_TABEL_NAME.equals(tagName)) {
			tabName = "";
		} else if (ITEM.equals(tagName)) {
			//Log.i(TAG, tabName);
			if (FoodsDatabaseHelper.CONFLICT_TABEL_NAME.equals(tabName)) {
				mConflictList.add(item);
			} else if (FoodsDatabaseHelper.UNSUITABLE_TABEL_NAME.equals(tabName)) {
				mUnsuitableList.add(item);
			}
		}
		
		tagName = "";
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String value = new String(ch, start, length);
		
		//Log.i(TAG, tabName + ", " + tagName + ", " + value);
		
		if (FoodsDatabaseHelper.CONFLICT_TABEL_NAME.equals(tabName)) {
			if (FoodsColumns.FOOD1.equals(tagName)
			 || FoodsColumns.FOOD2.equals(tagName)
			 || FoodsColumns.PRIORITY.equals(tagName)
			 || FoodsColumns.DETAIL.equals(tagName)) {
				//Log.i(TAG, tabName + ", " + tagName + ", " + value);
				//conflictItem.setDetail(value);
				item.put(tagName, value);
			}
		} else if (FoodsDatabaseHelper.UNSUITABLE_TABEL_NAME.equals(tabName)) {
			if (FoodsColumns.FOOD.equals(tagName) 
			 || FoodsColumns.PRIORITY.equals(tagName)
			 || FoodsColumns.DETAIL.equals(tagName)) {
				//Log.i(TAG, tabName + ", " + tagName + ", " + value);
				//unsuitableItem.setDetail(value);
				item.put(tagName, value);
			}
		}
	}

	public List<ContentValues> getConflictList() {
		return mConflictList;
	}

	public void setConflictList(List<ContentValues> conflictList) {
		mConflictList = conflictList;
	}

	public List<ContentValues> getUnsuitableList() {
		return mUnsuitableList;
	}

	public void setUnsuitableList(List<ContentValues> unsuitableList) {
		mUnsuitableList = unsuitableList;
	}
	
	
}
